<template>
	<div class="container">
		<div class="jx-container">
      <div class="jx-table">
        <div class="title">
          <img class="icon" src="/static/img/icon-platform.png"/>
          结算列表
          <div class="add-new">
            <el-button class="jx-btn" type="primary" @click="addRow">新增结算单位</el-button>
          </div>
        </div>
        <el-table :data="list" border style="width: 100%" v-loading="boo.loading">
          <el-table-column type="index" label="序列" width="60" align="center">
            <template slot-scope="scope">
              <span>{{(scope.$index + 1) + ((params_list.page - 1) * params_list.size)}}</span>
            </template>
          </el-table-column>
          <el-table-column prop="balanceUnitName" label="结算单位" width="180" align="center" :show-overflow-tooltip="true">
          </el-table-column>
          <el-table-column prop="balanceUnitProvince" label="适用地区" width="130" align="center" :show-overflow-tooltip="true">
          </el-table-column>
          <!-- <el-table-column prop="shopBalanceDay" label="店铺订单结算日" align="center" :show-overflow-tooltip="true">
          </el-table-column> -->
          <el-table-column prop="shopBalanceDay" label="商家服务单结算日" align="center" :show-overflow-tooltip="true">
          </el-table-column>
          <el-table-column prop="institutionBalanceDay" label="机构服务单结算日" align="center" :show-overflow-tooltip="true">
          </el-table-column>
          <el-table-column prop="balanceUnitStatus" label="状态" align="center" :show-overflow-tooltip="true">
          </el-table-column>
          <el-table-column label="操作" width="130" align="center">
            <template slot-scope="scope">
              <!-- <el-button @click="hiddenRow(scope.row)" type="text" size="small">{{scope.row.balanceUnitStatus == '启用' ? '禁用' : '恢复'}}</el-button> -->
              <el-button @click="deleteRow(scope.row)" type="text" size="small">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <div v-if="list.length != 0" class="jx-pagination">
          <el-pagination @current-change="pageChange" @size-change="handleSizeChange" :current-page="params_list.page" :page-sizes="[10,50,100]" :page-size="params_list.size" layout="total, sizes, prev, pager, next, jumper"
            :total="other.allRow">
          </el-pagination>
        </div>
      </div>
    </div>
    <!-- 新增结算单位 -->
    <el-dialog  :visible.sync="boo.showDialog" class="jx-dialog" width="65%">
      <div slot="title" class="header-title">
        <span class="title-name">{{currentRow.balanceUnitId ? '编辑结算单位' : '新增结算单位'}}</span>
      </div>
      <el-form ref="form" :model="currentRow" :rules="rules" label-width="150px" class="jx-form">
        <div class="jx-detail">
          <div class="detail-item">
            <div class="item">
              <el-form-item label="结算单位名称：" prop="balanceUnitName">
                <el-input v-model="currentRow.balanceUnitName" placeholder="请输入结算单位名称"></el-input>
              </el-form-item>
              <el-form-item label="适用地区：" prop="balanceUnitProvince">
                <el-checkbox-group v-model="currentRow.balanceUnitProvince">
                  <el-checkbox v-for="(item,index) in arealist" :key="index" :label="item">{{item}}</el-checkbox>
                </el-checkbox-group>
              </el-form-item>
              <!-- <el-form-item label="店铺订单结算日：" prop="text">
                <el-radio-group v-model="currentRow.text">
                  <el-radio :label="1">订单完成后立即结算</el-radio>
                  <el-radio :label="2">订单完成后第三天结算</el-radio>
                  <el-radio :label="3">订单完成后第7天结算</el-radio>
                </el-radio-group>
              </el-form-item> -->
              <el-form-item label="商家服务单结算日：" prop="shopBalanceDay">
                <el-input v-model="currentRow.shopBalanceDay" placeholder="请输入商家服务单结算日"></el-input>
              </el-form-item>
              <el-form-item label="机构服务单结算日：" prop="institutionBalanceDay">
                <el-input v-model="currentRow.institutionBalanceDay" placeholder="请输入机构服务单结算日"></el-input>
              </el-form-item>
            </div>
          </div>
        </div> 
      </el-form>
      <div class="btn-container">
        <el-button class="jx-btn" type="warning" @click="boo.showDialog = false">取消</el-button>
        <el-button class="jx-btn" type="primary" :loading="boo.saveing" @click="submitRow('form')">保存</el-button>
      </div>
    </el-dialog>
	</div>
</template>

<script>
	export default {
		data() {
			return {
        https: {
          list: this.$api.setting.settlement,
          common: this.$api.common.dict,
          area: this.$api.common.area,
        },
        boo: {
          loading: false,
          showDialog: false,
          saveing: false,
        },
        other: {
          allRow: 1,
        },
        list: '',
        currentRow: {balanceUnitProvince: []},
        params_list: {
          page: 1,
          size: 10,
        },
        arealist: '',
        rules: {
          shopBalanceDay: [{ required: true, message: '请输入商家服务单结算日', trigger: 'blur' }],
          balanceUnitName: [{ required: true, message: '请输入结算单位名称', trigger: 'blur' }],
          balanceUnitProvince: [{ required: true, message: '请选择适用地区', trigger: 'change' }],
          institutionBalanceDay: [{ required: true, message: '请输入机构服务单结算日', trigger: 'blur' }],
        },
			}
		},
		mounted(){
      let _self = this
      _self.getList()
      _self.getArea()
    },
		methods: {
      getArea(){
        let _self = this
        let success = (res)=>{
          res = res.data
          if(res.retCode == 1){
            _self.arealist = res.retObject
          }else{
            _self.$message.error(res.retMsg)
          }
        }
        _self.$axios.get(_self.https.area, {}, success) 
      },
      getList(){
        let _self = this
        let params = {
          pageNumber: _self.params_list.page,
          pageSize: _self.params_list.size,
          orderBy: '',
        }
        _self.boo.loading = true
        let success = (res)=>{
          res = res.data
          if(res.retCode == 1){
            _self.list = res.retObject.list
            _self.other.allRow = Number(res.retObject.totalRow)
          }else{
            _self.$message.error(res.retMsg)
          }
          _self.boo.loading = false
        }
        _self.$axios.get(_self.https.list.list, params, success) 
      },
      pageChange(val){
        let _self = this
        _self.params_list.page = val
        _self.getList()
      },
      handleSizeChange(val) {
        let _self = this
        _self.params_list.size = val
        _self.getList()
      },
      addRow(){
        let _self = this
        _self.boo.showDialog = true
        _self.currentRow = {balanceUnitProvince: []}
      },
      hiddenRow(item){
        let _self = this
      },
      deleteRow(item){
        let _self = this
        let success = (res)=>{
          res = res.data
          if(res.retCode == 1){
            _self.$message.success('删除成功')
            _self.getList()
          }else{
            _self.$message.error(res.retMsg)
          }
        }
        _self.$axios.delete(`${_self.https.list.delete}${item.balanceUnitId}`, {}, success)   
      },
      submitRow(formName){
        let _self = this
				this.$refs[formName].validate((vaild)=>{
					if(vaild){  
            _self.boo.saveing = true
            let params = {
              balanceUnitName: _self.currentRow.balanceUnitName,
              balanceUnitProvince: _self.currentRow.balanceUnitProvince,
              shopBalanceDay: _self.currentRow.shopBalanceDay,
              institutionBalanceDay: _self.currentRow.institutionBalanceDay,
            }    
            let success = (res)=>{
              res = res.data
              if(res.retCode == 1){
                _self.getList()
              }else{
                _self.$message.error(res.retMsg)
              }
              _self.boo.saveing = false
              _self.boo.showDialog = false
            }
            _self.$axios.post(_self.https.list.add, params, success)   
					}else{
						return false
					}
				})
      }
    }
	}

</script>
<style scoped lang="sass">
  .jx-table
    /deep/ .el-table__body td
      .cell
        height: auto
        line-height: auto
  .label-container
    margin-bottom: 8px
    .label-title
      width: 110px
      float: left
    .label-list
      margin-left: 110px
      .item
        margin-right: 30px
        width: auto
        /deep/ .el-checkbox
          margin-right: 3px
        .pointer
          cursor: pointer
          font-size: 14px
      .label-item
        padding: 10px
        background: #f4f4f4
        margin: 8px 0
        border-radius: 6px
        .item
          margin-top: 4px
</style>
